<template>
  <a-card>
    <a-row>
      <a-col :span="8">
        <a-statistic title="总阅读量" :value="statistic.viewCount"  >
          <template #suffix>
            <UserOutlined />
          </template>
        </a-statistic>
      </a-col>
      <a-col :span="8">
        <a-statistic title="总点赞量" :value="statistic.voteCount"  >
          <template #suffix>
            <like-outlined />
          </template>
        </a-statistic>
      </a-col>
      <a-col :span="8">
        <a-statistic title="点赞率"
                     :value="statistic.voteCount / statistic.viewCount * 100"
                     suffix="%"
                     :precision="2"
                     :value-style="{color: 'red'}"
        >
        </a-statistic>
      </a-col>
    </a-row>
  </a-card>

  <br/>
  <a-row>

    <a-col :span="12">
      <a-card>
        <a-row>
          <a-col :span="12">
            <a-statistic title="今日阅读" :value="statistic.todayViewCount"  >
              <template #suffix>
                <UserOutlined />
              </template>
            </a-statistic>

          </a-col>
          <a-col :span="12">
            <a-statistic title="今日点赞" :value="statistic.todayVoteCount"  >
              <template #suffix>
                <like-outlined />
              </template>
            </a-statistic>

          </a-col>
        </a-row>
      </a-card>
    </a-col>
    <a-col :span="12">
      <a-card>
        <a-row>
          <a-col :span="12">
            <a-statistic title="预计今日阅读" :value="statistic.todayViewIncrease"  >
              <template #suffix>
                <UserOutlined />
              </template>
            </a-statistic>


          </a-col>
          <a-col :span="12">

            <a-statistic title="预计今日阅读增长"
                         :value="statistic.todayViewIncreaseRateAbs"
                         suffix="%"
                         :precision="2"
                         :value-style="statistic.todayViewIncreaseRate < 0 ? { color: '#3f8600' } : { color: '#cf1322' }"
            >
              <template #prefix>
                <arrow-down-outlined v-if="statistic.todayViewIncreaseRate < 0"/>
                <arrow-up-outlined v-if="statistic.todayViewIncreaseRate >= 0"/>
              </template>
            </a-statistic>
          </a-col>

        </a-row>
      </a-card>
    </a-col>
  </a-row>

  <a-row>
    <a-col  :span="24">
      <div id="main" style="width: 100%;height: 300px;"></div>
    </a-col>
  </a-row>
</template>

<script>
import { onMounted,ref } from "vue"
import { UserOutlined,  LikeOutlined ,FileOutlined,ArrowUpOutlined, ArrowDownOutlined} from '@ant-design/icons-vue';
import axios from "axios"
export default {
  name: "EbookStatistic",
  components: {
    UserOutlined,
    LikeOutlined,
    FileOutlined,ArrowUpOutlined, ArrowDownOutlined
  },
  setup(){
    const statistic = ref({});

    const getstatistic = ()=>{
      console.log("getstatistic......")
      axios.get("api/ebookStatistic",).then(res=>{
        console.log(res)
        if (res.data.code === "200"){
          const statisticResp = res.data.data; // 是个数组
          statistic.value.viewCount = statisticResp[1].viewCount  // 总阅读数
          statistic.value.voteCount = statisticResp[1].voteCount  // 总点赞数
          statistic.value.todayViewCount = statisticResp[1].viewIncrease  // 今日阅读
          statistic.value.todayVoteCount = statisticResp[1].voteIncrease  // 今日点赞

          // 今天预计阅读数 = 今天现在阅读数 / 今天已过时间的百分率
          const now = new Date(); // 当前时间
          const nowRate = (now.getHours() * 60 + now.getMinutes()) / (60 * 24);
          statistic.value.todayViewIncrease = parseInt(String(statisticResp[1].viewIncrease / nowRate))

          // 今天预计增长率 = （今天预计-昨天） / 昨天
          statistic.value.todayViewIncreaseRate = (statistic.value.todayViewIncrease - statisticResp[0].viewIncrease) /  statisticResp[0].viewIncrease * 100;
          // 取绝对值
          statistic.value.todayViewIncreaseRateAbs = Math.abs(statistic.value.todayViewIncreaseRate)

          console.log("---",statistic.value)
        }else {
          message.error(res.data.msg);
        }
      })
    }

    const get30DayStatistic = ()=>{
      console.log("get30DayStatistic......")
      axios.get("api/ebook30DayStatistic",).then(res=>{
        console.log(res)
        if (res.data.code === "200"){
          const statisticList = res.data.data; // 是个数组
          get30DayStatisticEcharts(statisticList)
        }else {
          message.error(res.data.msg);
        }
      })
    }

    // const testEcharts = () =>{
    //   // 基于准备好的dom，初始化echarts实例
    //   const myChart = echarts.init(document.getElementById('main'));
    //
    //   // 指定图表的配置项和数据
    //   const option = {
    //     title: {
    //       text: 'ECharts 入门示例'
    //     },
    //     tooltip: {},
    //     legend: {
    //       data: ['销量']
    //     },
    //     xAxis: {
    //       data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
    //     },
    //     yAxis: {},
    //     series: [
    //       {
    //         name: '销量',
    //         type: 'bar',
    //         data: [5, 20, 36, 10, 10, 20]
    //       }
    //     ]
    //   };
    //
    //   // 使用刚指定的配置项和数据显示图表。
    //   myChart.setOption(option);
    // }

    const get30DayStatisticEcharts = (statisticList) =>{

      const x=[]
      const yView=[]
      const yVote=[]
      for (let i=0; i< statisticList.length; i++){
        const item = statisticList[i]
        x.push(item.date)
        yView.push(item.viewIncrease)
        yVote.push(item.voteIncrease)
      }

      // 基于准备好的dom，初始化echarts实例
      const myChart = echarts.init(document.getElementById('main'));

      // 指定图表的配置项和数据
      const option = {
        title: {
          text: 'Stacked Line'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['阅读数', '点赞数']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: x
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '阅读数',
            type: 'line',
            stack: 'Total',
            data: yView
          },
          {
            name: '点赞数',
            type: 'line',
            stack: 'Total',
            data: yVote
          },
        ]
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    }


    onMounted(()=>{
      getstatistic()
      // testEcharts()
      get30DayStatistic()
    })

    return{
      statistic,
      get30DayStatistic,
      get30DayStatisticEcharts
      // c
    }
  }
}
</script>

<style scoped>

</style>